// pages/home/home.js
const app = getApp();
const user = require('../../utils/user.js');
const api = require('../../config/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadImgShow:false
  },

  goToModel(e) {
    let type = e.currentTarget.dataset.type;
    let that = this;
    if (app.globalData.hasLogin) {
      if (type == 1) {
        that.threeUpload()
      } else {
        that.appointment()
      }
    } else {
      user.checkRegister().then(res => {
        console.log('注册成功', res)
        if (res) {
          if (type == 1) {
            that.threeUpload()
          } else {
            that.appointment()
          }
        }
      }).catch(e => {})
    }

  },
  // 上传图片获取3D人像
  threeUpload() {
    let that=this;
    that.setData({
      uploadImgShow:true
    })

    
  },
  onSuccess() {
    this.onClose();
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album','camera'],
      // maxDuration: 30,
      // camera: 'back',
      success(res) {
        console.log('单照片合成', res)

        wx.showLoading({
          title: '生成中预计30秒',
        })

        // 上传头像
        wx.uploadFile({
          url: api.ThreeUpload,
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          formData: {
            openid: wx.getStorageSync('openid')
          },
          success(res) {
            console.log('合成成功', res)
            if (res.statusCode == 200) {
              let resData = JSON.parse(res.data);
              if (resData.error_code == 0) {
                // 上传成功
                let modelSrc = encodeURIComponent('https://ders-1312669922.cos.ap-guangzhou.myqcloud.com/584a68eba826b342ec671f5e6ce21605/22/avatar/model.gltf');
                modelSrc = encodeURIComponent(resData.data.xmimg)
                wx.navigateTo({
                  url: '/pages/preview/single?modelId=' + resData.data.id + '&modelSrc=' + modelSrc,
                })

              } else {
                wx.showToast({
                  icon: 'none',
                  title: '生成3D人像失败',
                })
                reject(false);
              }
            } else {
              wx.showToast({
                icon: 'none',
                title: '上传照片失败，请稍后重试',
              })
              reject(false);
            }
            //do something
          },
          fail: (err) => {
            //上传失败：修改pedding为reject
            console.log('头像上传失败：', err)
            wx.showToast({
              title: "网络出错，上传失败，请稍后重试",
              icon: 'none'
            });
            reject(false)
          }
        })


      },
      fail(e) {
        wx.showToast({
          icon: 'none',
          title: '取消上传照片',
        })
        console.log('失败', e)
      }
    })

  },
  onClose() {
    this.setData({ uploadImgShow: false });
  },

  // 预约
  appointment() {
    wx.navigateTo({
      url: '/pages/store/store',
    })
  },

  // 产品介绍
  goToProduct(e) {
    let that = this;
    let name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/home/product/product?name=' + name,
    })
  },

  goToYxzb(){
    wx.navigateToMiniProgram({
      appId: 'wxeb08f4edddf54030',
      // path: 'page/index/index?id=123',
      // 额外参数
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})